<template>
  <el-card class='box-card' shadow='never'>
    <template #header>
      <div class='card-header'>
        <span>提交信息</span>
      </div>
    </template>
    <div class='mb-3'>
      <el-button type='primary' :icon='CopyDocument' @click='copyForm'>复制</el-button>
      <el-button type='danger' ghost :icon='Plus' @click='addForm'>添加</el-button>
    </div>
    <el-row :gutter='20'>
      <el-col :span='6' v-for='(data, index) in formArr' :key='index' style='position: relative;'>
        <div :class="index != 0 ? 'custcard' : ''" @click='delForm(index)'>
        </div>
        <el-card shadow='never' class='delaydisappear'>
          <el-form :model='data' label-width='120px'
                   style='max-width: 460px' status-icon>
            <el-form-item label='微信ID' prop='wechat_id'
                          :rules="[
        { required: true, message: '微信ID必填' },
      ]"
            >
              <el-input v-model='data.wechat_id' />
              <el-button size='small' @click='checkWechatId(data)'>检查</el-button>
              <span class='text-red-400' v-if='data.is_exist'>存在</span>
              <span class='text-green-400' v-else>暂无</span>
            </el-form-item>
            <el-form-item label='视频号ID' prop='video_id'
                          :rules="[
        { required: true, message: '视频号ID必填' },
      ]">
              <el-input v-model='data.video_id' />
            </el-form-item>
            <el-form-item label='视频号昵称' prop='video_nickname'>
              <el-input v-model='data.video_nickname' />
            </el-form-item>
            <el-form-item label='成交截图' prop='order_img'
                          :rules="[
        { required: true, message: '成交截图必填' },
      ]">
              <hd-upload-single-image v-model='data.order_img'></hd-upload-single-image>
            </el-form-item>
            <el-form-item label='备注' prop='remark'>
              <el-input v-model='data.remark' type='textarea' :autosize='{ minRows: 2, maxRows: 4 }' />
            </el-form-item>
            <el-form-item>
              <el-button type='primary' @click='onSubmit(index,data)'>提交</el-button>
            </el-form-item>
          </el-form>
        </el-card>

      </el-col>
    </el-row>

  </el-card>
</template>

<script lang='ts' setup>
const { store, recordCheck } = useRecord()
import type { FormInstance, FormRules } from 'element-plus'
import { ElMessageBox } from 'element-plus'
import { CopyDocument, Plus } from '@element-plus/icons-vue'

let formState = {
  id: 0,
  user_id: 0,
  wechat_id: '',
  video_id: '',
  video_nickname: '',
  order_img: '',
  remark: '',
  canupdate: false,
  expired_at: '',
  created_at: '',
  updated_at: '',
} as RecordModel
let newFormData = {
  id: 0,
  user_id: 0,
  wechat_id: '',
  video_id: '',
  video_nickname: '',
  order_img: '',
  remark: '',
  canupdate: false,
  expired_at: '',
  created_at: '',
  updated_at: '',
} as RecordModel
let formArr = ref<RecordModel[]>([formState])
const formRef = ref<FormInstance>()
const addForm = () => {
  if (formArr.value.length >= 8) {
    ElMessageBox.alert('已存在8个表单，无法提交新表单')
    return
  }
  const copiedForm = JSON.parse(JSON.stringify(newFormData)) // Deep copy the form data
  formArr.value.push(copiedForm)
}
const copyForm = () => {
  if (formArr.value.length >= 8) {
    ElMessageBox.alert('已存在8个表单，无法提交新表单')
    return
  }
  let newForm = {
    id: 0,
    user_id: 0,
    wechat_id: formArr.value[0].wechat_id,
    video_id: '',
    video_nickname: '',
    order_img: '',
    remark: '',
    is_exist: false,
    canupdate: false,
    expired_at: '',
    created_at: '',
    updated_at: '',
  }

  const copiedForm = JSON.parse(JSON.stringify(newForm)) // Deep copy the form data
  formArr.value.push(copiedForm)
}

const rules = reactive<FormRules<RecordModel>>({
  wechat_id: [
    {
      required: true,
      message: '请填写微信ID',
      trigger: 'change',
    },
  ],
  video_id: [
    {
      required: true,
      message: '请上传视频ID',
      trigger: 'change',
    },
  ],
  order_img: [
    {
      required: true,
      message: '请上传截图',
      trigger: 'change',
    },
  ],
})
const onSubmit = async (index: any, data: any) => {
  await store(data).then(_ => {
    //清空表单
    Object.keys(data).forEach(key => {
      data[key] = ''
    })
  })
}
const checkWechatId = async (data: RecordModel) => {
  if (data?.wechat_id == '') {
    ElMessageBox.alert('请输入微信ID')
    return
  }
  const { is_exist } = await recordCheck(data.wechat_id)
  data.is_exist = is_exist
}

const delForm = (index: number) => {
  formArr.value.splice(index, 1)
}
</script>
<style lang='scss' scoped>
.custcard {
  position: absolute;
  right: -10px;
  top: -10px;
  width: 40px;
  height: 40px;
  border-radius: 20px;
  background-color:#d83b01;
  cursor: pointer;
  &:before {
    position: absolute;
    top:15px;
    left:5px;
    content: "";
    width: 30px;
    height: 10px;
    background: white;
    transform: rotate(45deg);
  }
  &::after {
    position: absolute;
    top:15px;
    left:5px;
    content: "";
    width: 30px;
    height: 10px;
    background: white;
    transform: rotate(-45deg);
  }
  &:hover {
    transform: rotate(180deg);
    transition: all 0.5s;
  }

}


.delaydisappear {
  //  出现或者消失时的动画，原生css延时500ms;
  transition: all 0.5s;
}
</style>